package com.zsqat.eshop.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.zsqat.common.utils.R;
import com.zsqat.eshop.product.entity.SkuImagesEntity;
import com.zsqat.eshop.product.entity.SpuInfoDescEntity;
import com.zsqat.eshop.product.feign.SeckillFeignService;
import com.zsqat.eshop.product.service.*;
import com.zsqat.eshop.product.vo.SeckillSkuVo;
import com.zsqat.eshop.product.vo.SkuItemSaleAttrVo;
import com.zsqat.eshop.product.vo.SkuItemVo;
import com.zsqat.eshop.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;
import com.zsqat.eshop.product.dao.SkuInfoDao;
import com.zsqat.eshop.product.entity.SkuInfoEntity;

import javax.annotation.Resource;

@Service("skuInfoService" )
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
  // 线程池的核心类
  @Resource
  private ThreadPoolExecutor executor;
  @Resource
  private SkuSaleAttrValueService skuSaleAttrValueService;
  @Resource
  private SpuInfoDescService spuInfoDescService;
  @Resource
  private AttrGroupService attrGroupService;
  @Resource
  private SkuImagesService skuImagesService;
  @Autowired
  private SeckillFeignService seckillFeignService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<SkuInfoEntity> page = this.page(
        new Query<SkuInfoEntity>().getPage(params),
        new QueryWrapper<>()
    );

    return new PageUtils(page);
  }

  @Override
  public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
    SkuItemVo skuItemVo = new SkuItemVo();
    CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
      // 1.sku基本信息的获取  pms_sku_info
      SkuInfoEntity info = this.getById(skuId);
      skuItemVo.setInfo(info);
      return info;
    }, executor);


    CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
      // 2.获取spu的销售属性组合
      List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
      skuItemVo.setSaleAttr(saleAttrVos);
    }, executor);


    CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
      // 3.获取spu的介绍    pms_spu_info_desc
      SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
      skuItemVo.setDesc(spuInfoDescEntity);
    }, executor);


    CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res) -> {
      // 4.获取spu的规格参数信息
      List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCategoryId());
      skuItemVo.setGroupAttrs(attrGroupVos);
    }, executor);


    // 5.sku的图片信息    pms_sku_images
    CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
      List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId);
      skuItemVo.setImages(imagesEntities);
    }, executor);


    CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
      // 6.远程调用查询当前sku是否参与秒杀优惠活动
      R skuSeckilInfo = seckillFeignService.getSkuSeckilInfo(skuId);
      if (skuSeckilInfo.getCode() == 0) {
        // 查询成功
        SeckillSkuVo seckilInfoData = skuSeckilInfo.getData("data", new TypeReference<SeckillSkuVo>() {
        });
        skuItemVo.setSeckillSkuVo(seckilInfoData);

        if (seckilInfoData != null) {
          long currentTime = System.currentTimeMillis();
          if (currentTime > seckilInfoData.getEndTime()) {
            skuItemVo.setSeckillSkuVo(null);
          }
        }
      }
    }, executor);


    //等到所有任务都完成
    CompletableFuture.allOf(saleAttrFuture,
        descFuture,
        baseAttrFuture,
        imageFuture,
        seckillFuture).get();
    return skuItemVo;
  }
}